import json
import os.path
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter.ttk import Checkbutton
import threading
import CovertBili2Mp4Acc
import common
import JsonObject


class FolderSelectorApp:
    def __init__(self, root, cfg):
        self.root = root
        self.root.title("文件夹处理工具")
        self.root.geometry("500x500")

        # 变量初始化
        self.cfg = cfg
        if self.cfg is None:
            self.cfg = BiliBiliCfg()
        self.input_folder = tk.StringVar()
        self.input_folder.set(self.cfg.BiliBiliDir or "")
        self.output_folder = tk.StringVar()
        self.output_folder.set(self.cfg.OutPutDir or "")

        # UI布局
        self.create_widgets()

    def create_widgets(self):
        # 输入文件夹选择
        tk.Label(self.root, text="输入文件夹:").pack(pady=(20, 5))
        tk.Entry(self.root, textvariable=self.input_folder, width=50).pack()
        tk.Button(self.root, text="浏览...", command=self.select_input_folder).pack(pady=5)

        # 输出文件夹选择
        tk.Label(self.root, text="输出文件夹:").pack(pady=(15, 5))
        tk.Entry(self.root, textvariable=self.output_folder, width=50).pack()
        tk.Button(self.root, text="浏览...", command=self.select_output_folder).pack(pady=5)

        # 功能按钮
        btn_frame = tk.Frame(self.root)
        btn_frame.pack(pady=20)

        tk.Button(
            btn_frame,
            text="开始处理",
            command=self.start_processing,
            width=15
        ).pack(side=tk.LEFT, padx=10)

        tk.Button(
            btn_frame,
            text="退出",
            command=self.root.quit,
            width=15
        ).pack(side=tk.LEFT, padx=10)

    def select_input_folder(self):
        folder = filedialog.askdirectory(title="选择输入文件夹")
        if folder:
            self.input_folder.set(folder)
            self.cfg.BiliBiliDir = folder
            save_config(self.cfg)

    def select_output_folder(self):
        folder = filedialog.askdirectory(title="选择输出文件夹")
        if folder:
            self.output_folder.set(folder)
            self.cfg.OutPutDir = folder
            save_config(self.cfg)

    def start_processing(self):
        if not self.input_folder.get() or not self.output_folder.get():
            messagebox.showerror("错误", "请先选择输入和输出文件夹")
            return

        messagebox.showinfo(
            "处理信息",
            f"将从 {self.input_folder.get()} 处理到 {self.output_folder.get()}\n"
        )
        # 这里添加实际处理逻辑
        thread = threading.Thread(target=self.cbtask)
        thread.start()


    def cbtask(self):
        cb = CovertBili2Mp4Acc.CovertBili2Mp4Acc(self.cfg.BiliBiliDir, self.cfg.OutPutDir)
        cb.deal()



class BiliBiliCfg:
    # 类属性（所有实例共享）
    OutPutDir = ""
    BiliBiliDir = ""

def load_config():
    cfg_name = "./config.json"
    cfg = JsonObject.JsonObject.from_file(cfg_name)
    return cfg
    pass
def save_config(cfg):
    if cfg is None:
        return
    cfg_name = "./config.json"
    with open(cfg_name, 'w', encoding='utf-8') as f:
        json.dump(cfg.__dict__, f, indent=4)

if __name__ == "__main__":
    cfg = load_config()
    root = tk.Tk()
    app = FolderSelectorApp(root, cfg)
    root.mainloop()